package io.renren.common.utils;

import io.renren.common.exception.RRException;
import org.jdom.JDOMException;
import org.xmlpull.v1.XmlPullParserException;
import sun.applet.Main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Map;

public class SocketUtils {

    /***
     * 调用Socket
     * @param ipAddress
     * @param port
     * @param sRequest
     * @return
     * @throws RRException
     */
    public static String sendSocketRequest(String ipAddress, int port, String sRequest) throws RRException {
        BufferedReader ins = null;
        PrintWriter outs = null;
        Socket socket = null;

        try {
            StringBuffer sb = new StringBuffer();
            socket = new Socket(ipAddress, port);
            ins = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GB2312"));
            outs = new PrintWriter(socket.getOutputStream());
            outs.print(sRequest);
            outs.flush();
            socket.shutdownOutput();


            String sLine = null;
            while ((sLine = ins.readLine()) != null) {
                sb.append(sLine);
            }
            return sb.toString();
        } catch (IOException e) {
            throw new RRException("异常：" + e.getMessage());
        } finally {
            try {
                if (ins != null) {
                    ins.close();
                }
                if (outs != null) {
                    outs.close();
                }
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        try {
            String sRequest = "<?xml version=\"1.0\" encoding=\"GB2312\" standalone=\"yes\" ?>  \n" +
                    "<TX>  \n" +
                    "  <REQUEST_SN>01</REQUEST_SN>  \n" +
                    "  <CUST_ID>105521000005026</CUST_ID>  \n" +
                    "  <USER_ID>105521000005026-009</USER_ID>  \n" +
                    "  <PASSWORD>li234567</PASSWORD>  \n" +
                    "  <TX_CODE>5W1002</TX_CODE>  \n" +
                    "  <LANGUAGE>CN</LANGUAGE>  \n" +
                    "  <TX_INFO>  \n" +
                    "    <START>20190101</START>  \n" +
                    "    <STARTHOUR>00</STARTHOUR>  \n" +
                    "    <STARTMIN>00</STARTMIN>  \n" +
                    "    <END>20190101</END>  \n" +
                    "    <ENDHOUR>00</ENDHOUR>  \n" +
                    "    <ENDMIN>00</ENDMIN>  \n" +
                    "    <KIND>1</KIND>  \n" +
                    "    <ORDER>TNO20191114155239740</ORDER>  \n" +
                    "    <ACCOUNT></ACCOUNT>  \n" +
                    "    <DEXCEL></DEXCEL>  \n" +
                    "    <MONEY></MONEY>  \n" +
                    "    <NORDERBY>2</NORDERBY>  \n" +
                    "    <PAGE></PAGE>  \n" +
                    "    <POS_CODE></POS_CODE>  \n" +
                    "    <STATUS>3</STATUS>  \n" +
                    "  </TX_INFO>  \n" +
                    "</TX>";

            String resxml = SocketUtils.sendSocketRequest("127.0.0.1", 12345, sRequest);
            System.out.println("返回结果集：" + resxml);
            Map<String, String> restmap = XmlUtil.doXMLParse(resxml);
            Map<String, String> txInfo = XmlUtil.doXMLParse("<?xml version=\"1.0\" encoding=\"GB18030\"?><TX_INFO>"+restmap.get("TX_INFO")+"</TX_INFO>");
            Map<String, String> list = XmlUtil.doXMLParse("<?xml version=\"1.0\" encoding=\"GB18030\"?><LIST>"+txInfo.get("LIST")+"</LIST>");

            System.out.println("======================================");

        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
